package kits.ftp;

import java.io.File;
import java.io.IOException;

import cn.hutool.core.date.DateTime;
import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j
public class FtpUpdate {
	private FTPClient ftp;

	public FtpUpdate() {
		FtpConfig ftpConfig = new FtpConfig();
		ftpConfig.setIp("120.24.0.160");
		ftpConfig.setPort(21);
		ftpConfig.setUserName("kod");
		ftpConfig.setPassword("kod");
		ftp = new FtpKit(ftpConfig).getClient();
	}
	
	public boolean uploadFile(String path,File next) {
		try {
			try {
				ftp.changeDirectory(path);
				ftp.upload(next);
			} catch (IOException e) {
				e.printStackTrace();
			}
			log.info("上传文件成功:" + next.getName() + " 最后修改" + new DateTime(next.lastModified()));
			return true;
		} catch (IllegalStateException | FTPIllegalReplyException | FTPException | FTPDataTransferException
				| FTPAbortedException e) {
			log.info("----上传文件失败:" + next.getName());
		}
		return false;
	}
}
